<template>
  <div class="dept-container">
    <el-card class="box-card" shadow="never">
      <div slot="header" class="clearfix">
        <el-button type="primary" size="small " icon="el-icon-plus" @click="handleAdd">添加部门</el-button>
      </div>
      <el-table border stripe :data="deptList">
        <el-table-column type="index" label="序号" align="center" width="100"></el-table-column>
        <el-table-column prop="name" label="部门名称" align="center"></el-table-column>
        <el-table-column prop="updateTime" label="更新时间" align="center"></el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <el-button type="text" icon="el-icon-edit" @click="handleEdit(row)">编辑</el-button>
            <el-button type="text" icon="el-icon-delete" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageParams.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>

    <el-dialog :title="title" :visible.sync="dialogVisible" width="40%" @close="dialogClose">
      <el-form label-width="80px" size="small" :modal="formData" :rules="formRules" ref="formRef">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入部门名称"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button size="small" @click="cancel">取 消</el-button>
        <el-button size="small" type="primary" @click="handleSubmit">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>

import { mapState } from 'vuex'

export default {
  name: 'Dept',
  data() {
    return {
      formRules: {
        name: [
          { required: false, message: '请输入部门名称', trigger: 'blur' }
        ]
      },
      formData: {
        name: ''
      },
      formRef: null,
      edit: false,
      dialogVisible: false,
      pageParams: {
        page: 1,
        pageSize: 5
      }
    }
  },
  methods: {
    async handleSizeChange(pageSize) {
      this.pageParams.pageSize = pageSize
      await this.$store.dispatch('deptStore/fetchDeptAction', this.pageParams)
    },
    async handleCurrentChange(page) {
      this.pageParams.page = page
      await this.$store.dispatch('deptStore/fetchDeptAction', this.pageParams)
    },
    handleDelete(row) {
      this.$confirm(`此操作将永久删除${row.name}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          const deleteResult = await this.$store.dispatch('deptStore/deleteDeptAction', row.id)
          await this.$store.dispatch('deptStore/fetchDeptAction', this.pageParams)
          this.$message.success(deleteResult)
        } catch (error) {
          this.$message.error(error.message)
        }
      }).catch(() => {

      })
    },
    handleAdd() {
      this.edit = false
      this.dialogVisible = true

    },
    async handleEdit(row) {
      this.edit = true
      this.dialogVisible = true
      try {
        const data = await this.$store.dispatch('deptStore/getDeptDetailAction', row.id)
        this.formData = data
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    async handleSubmit() {
      if (this.edit) {
        try {
          const updateResult = await this.$store.dispatch('deptStore/updateDeptAction', this.formData)
          await this.$store.dispatch('deptStore/fetchDeptAction', this.pageParams)
          this.$message.success(updateResult)
          this.dialogVisible = false
        } catch (error) {
          this.$message.error(error.message)
        }
      } else {
        try {
          const addResult = await this.$store.dispatch('deptStore/addDeptAction', this.formData)
          await this.$store.dispatch('deptStore/fetchDeptAction', this.pageParams)
          this.$message.success(addResult)
          this.dialogVisible = false
        } catch (error) {
          this.$message.error(error.message)
        }
      }
    },
    cancel() {
      this.dialogVisible = false
    },
    dialogClose() {
      this.$refs.formRef.resetFields()
    }
  },
  mounted() {
    this.$store.dispatch('deptStore/fetchDeptAction', this.pageParams)
  },
  computed: {
    title() {
      return this.edit ? '编辑部门' : '添加部门'
    },
    ...mapState('deptStore', ['deptList', 'total'])
  }
}
</script>

<style lang="scss" scoped>
.dept-container {
  padding: 15px;

  .el-table {
    margin-bottom: 15px;
  }

  .el-pagination {
    display: flex;
    justify-content: center;
  }
}
</style>
